<?php
namespace Operationtest;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

use Operationtest\Model\StoreTable;
use Operationtest\Model\SaleheadTable;
use Operationtest\Model\SalerowTable;
use Operationtest\Model\MeteoTable;
use Operationtest\Model\MeteotypeTable;
use Operationtest\Model\StorecountTable;
use Operationtest\Model\DrawercountTable;
use Operationtest\Model\MarcheTable;
use Operationtest\Model\MarchestoreTable;
use Operationtest\Model\BudgetTable;
use Operationtest\Model\RhdataTable;
use RHX\Model\Table\OperationTable;
use RHX\Model\Table\SiteTable;
use RHX\Model\Manager\OperationManager;

class Module
{ 
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        
        $sharedEvents = $eventManager->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
            $controller = $e->getTarget();
            $route = $controller->getEvent()->getRouteMatch();
            $e->getViewModel()->setVariables(
                array('controllerName'=> $route->getParam('__CONTROLLER__', 'index'),
                    'actionName' => $route->getParam('action', 'index'),
                    'moduleName' => strtolower(__NAMESPACE__))
            );
        }, 100);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
                ),
            ),
        );
    }
    
    public function getServiceConfig()
    {
        return array(
            'factories' => array(    
                // Service for Store table
                'Operationtest\Model\StoreTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new StoreTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Salehead table
                'Operationtest\Model\SaleheadTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new SaleheadTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Salerow table
                'Operationtest\Model\SalerowTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new SalerowTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Meteo table
                'Operationtest\Model\MeteoTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new MeteoTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Meteotype table
                'Operationtest\Model\MeteotypeTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new MeteotypeTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Storecount table
                'Operationtest\Model\StorecountTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new StorecountTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Drawercount table
                'Operationtest\Model\DrawercountTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new DrawercountTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Marche table
                'Operationtest\Model\MarcheTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new MarcheTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Marchestore table
                'Operationtest\Model\MarchestoreTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new MarchestoreTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for Budget table
                'Operationtest\Model\BudgetTable' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new BudgetTable($dbAdapter);
                    return $table;
                },
                        
                // Service for Rhdata table
                'Operationtest\Model\RhdataTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new RhdataTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for MO request
                'RHX\Model\Table\OperationTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');                    
                    $table = new OperationTable($dbAdapter);
                    return $table;
                }, 
                        
                // Service for RH Store table
                'RHX\Model\Sitetable' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new SiteTable($dbAdapter);
                    return $table;
                },           
                        
                // Service for RH OperationManager table
                'RHX\Model\Manager\OperationManager' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $dbAdapter2 = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new OperationManager($dbAdapter, $dbAdapter2);
                    return $table;
                },
            ),
        );
    }
}
